<?php
namespace app\admin\controller;

use app\admin\model\SystemAdmin;
use app\admin\model\SystemMenu;
use app\admin\BaseController;
use app\admin\model\SystemRole;
use think\facade\Db;
use think\facade\Log;
use think\App;
use think\facade\View;
use think\facade\Request;

class Admin extends BaseController
{
    /**
     * 列表页面
     * @return string
     */
    public function index()
    {
        return View::fetch('index/admin');
    }

    /**
     * 获取管理员列表
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(){
        [$page,$limit] = $this->getPage();
        $getData = Request::post();
        $systemAdminModel = new SystemAdmin();
        $backData = $systemAdminModel->getAdminList($getData,$page,$limit);
        $backData['code'] = 0;
        $backData['msg'] = '获取成功';
        return json()->data($backData);
    }

    /**
     * 添加管理员表单
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addAdminForm(){
        $systemRoleModel = new SystemRole();
        $roleList = $systemRoleModel->getList();
        View::assign('roleList', $roleList);
        return View::fetch('index/addAdmin');
    }

    /**
     * 添加管理员
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addAdminData(){
        $getData = Request::post();
        if(!$getData['account']) return $this->fail('请输入账号');
        if(!$getData['real_name']) return $this->fail('请输入姓名');
        if(!$getData['password']) return $this->fail('请输入密码');
        if($getData['password'] != $getData['confirmPassword']) return $this->fail('输入密码不一致');
        $systemAdminModel = new SystemAdmin();
        $adminInfo = $systemAdminModel->getAdminInfoByAccount($getData['account']);
        if($adminInfo) return $this->fail('账号已存在');
        $result = $systemAdminModel->addData($getData);
        if($result) return $this->success('添加成功');
        return $this->fail('添加失败');
    }

    /**
     * 编辑管理员
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editAdminForm(){
        $adminId = Request::get('admin_id');
        $systemAdminModel = new SystemAdmin();
        $adminInfo = $systemAdminModel->getAdminInfoById($adminId);
        $adminRole = [];
        $adminStatus = 0;
        if($adminInfo){
            $adminInfo = $adminInfo->toArray();
            $adminRole = explode(",",$adminInfo['roles']);
            $adminStatus = 1;
        }
        $systemRoleModel = new SystemRole();
        $roleList = $systemRoleModel->getList();
        foreach ($roleList as $key=>$value){
            if(in_array($value['role_id'],$adminRole)){
                $roleList[$key]['selected'] = 'selected';
            }else{
                $roleList[$key]['selected'] = '';
            }
        }
        View::assign('roleList', $roleList);
        View::assign('adminInfo', $adminInfo);
        View::assign('adminStatus', $adminStatus);
        return View::fetch('index/editAdmin');
    }

    /**
     * 修改管理员
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function updateAdmin(){
        $getData = Request::post();
        if(!$getData['account']) return $this->fail('请输入账号');
        if(!$getData['real_name']) return $this->fail('请输入姓名');
        if($getData['password'] && $getData['password'] != $getData['confirmPassword']) return $this->fail('输入密码不一致');
        $systemAdminModel = new SystemAdmin();
        $adminInfo = $systemAdminModel->getAdminInfoById($getData['admin_id']);
        if(!$adminInfo)  return $this->fail('请选择要正确要修改管理员');
        if(in_array(1,explode(",",$adminInfo['roles']))){
            $getData['role_id'] = [1];
        }
        //账号判断
        $checkInfo = $systemAdminModel->getAdminInfoByAccount($getData['account']);
        if($checkInfo && $getData['admin_id'] != $checkInfo['admin_id']) return $this->fail('账号已存在');

        $result = $systemAdminModel->editData($getData);
        if($result) return $this->success('修改成功');
        return $this->fail('未作任何修改');
    }

    /**
     * 删除管理员
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function deleteAdmin(){
        $adminId = Request::post('admin_id');
        if(!$adminId) return $this->fail('请选择要删除的管理员');
        $systemAdminModel = new SystemAdmin();
        $adminInfo = $systemAdminModel->getAdminInfoById($adminId);
        if(!$adminInfo)  return $this->fail('请选择正确的管理员');
        if(in_array(1,explode(",",$adminInfo['roles']))){
            return $this->fail('超级管理员不能删除');
        }
        $saveData = [
            'is_del'=>1,
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        $result = $systemAdminModel->where('admin_id',$adminId)->update($saveData);
        if($result) return $this->success('删除成功');
        return $this->fail('删除失败');
    }

}


